package com.shxt.hotel.background;
import com.shxt.hotel.util.DBManager;
import com.shxt.hotel.util.Time;
import java.util.Map;
import java.util.List;
import java.util.HashMap;

import com.shxt.hotel.conf.Config;

public class User {
    
    public static final int SUCCESS = 0;
    public static final int STAFF_NAME_ERROR = 1;
    public static final int PASSWORD_ERROR = 2;
    
    private static final String TABLE_NAME = "user";
    private static final String PERMISSION_TABLE_NAME = "user_permission";
    
    private int user_id = -1;
    
    DBManager dbm;
    
    public User()
    throws Exception{
        try{
            dbm = new DBManager(Config.DB_NAME, Config.DB_USERNAME, Config.DB_PASSWORD);
            
        }catch(Exception e){
            throw e;
        }
    }
    
    public List<Map<String, Object>> getUserList()
    throws Exception{
        try{
            String sql = "select * from " + User.TABLE_NAME;
            return dbm.executeQuery(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int login(String username, String password)
    throws Exception{
        try{
            String sql = "select * from " + User.TABLE_NAME + " where username = '" + username + "'";
            List<Map<String, Object>> userList = dbm.executeQuery(sql);
            System.out.println("sql list size: " + userList.size());
            
            if(userList.size() == 0){
                return User.STAFF_NAME_ERROR;
            }
            if(!userList.get(0).get("password").equals(password)){
                return User.PASSWORD_ERROR;
            }
            
            this.user_id = (int)userList.get(0).get("id");
            return User.SUCCESS;
            
        }catch(Exception e){
            throw e;
        }
    }
    
    public int register(Map<String, String[]> paraMap)
    throws Exception{
        try{
            Map<String, Object> newParaMap = dbm.packageParaMap(paraMap);
            
            newParaMap.put("user_permission", 0);
            newParaMap.put("create_date", Time.getDate());
            newParaMap.put("black_list", 0);
            newParaMap.remove("repassword");
            
            return dbm.insert(User.TABLE_NAME, newParaMap);
        }catch(Exception e){
            throw e;
        }
    }
    
    public Boolean checkIdCard(String id_card)
    throws Exception{
        try{
            String sql = "select id from " + User.TABLE_NAME + " where id_card = '" + id_card + "'";
            int rslt = dbm.executeQuery(sql).size();
            return (rslt > 0);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int edit(Map<String, String[]> paraMap)
    throws Exception{
        try{
            Map<String, Object> newParaMap = dbm.packageParaMap(paraMap);
            Map<String, Object> whereMap = new HashMap<String, Object>();
            whereMap.put("id", newParaMap.get("id"));
            newParaMap.remove("id");
            newParaMap.remove("old_password");
            newParaMap.remove("re_password");
            
            return dbm.update(User.TABLE_NAME, newParaMap, whereMap);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int getUserId()
    throws Exception{
        try{
            return this.user_id;
        }catch(Exception e){
            throw e;
        }
    }
    
    public Map<String, Object> getUserInfo(int id)
    throws Exception{
        try{
            String sql = "select * from " + User.TABLE_NAME + " where id = " + id;
            return dbm.executeQuery(sql).get(0);
        }catch(Exception e){
            throw e;
        }
    }
    
    public List<Map<String, Object>> getPermissionList()
    throws Exception{
        try{
            String sql = "select * from " + User.PERMISSION_TABLE_NAME;
            return dbm.executeQuery(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public Map<String, Object> getPermission(int pid)
    throws Exception{
        try{
            String sql = "select * from " + User.PERMISSION_TABLE_NAME + " where id = " + pid;
            return dbm.executeQuery(sql).get(0);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int setUserPerm(int uid, int pid)
    throws Exception{
        try{
            String sql = "update " + User.TABLE_NAME + " set user_permission = " + pid + " where id = " + uid;
            return dbm.executeUpdate(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int addBlacklist(int uid)
    throws Exception{
        try{
            String sql = "update " + User.TABLE_NAME + " set black_list = 1 where id = " + uid;
            return dbm.executeUpdate(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public int cancelBlacklist(int uid)
    throws Exception{
        try{
            String sql = "update " + User.TABLE_NAME + " set black_list = 0 where id = " + uid;
            return dbm.executeUpdate(sql);
        }catch(Exception e){
            throw e;
        }
    }
    
    public void close()
    throws Exception{
        try{
            dbm.close();
        }catch(Exception e){
            throw e;
        }
    }
    
}
